package net.javaw.callplatform;

import net.javaw.callplatform.util.DialogUtil;
import net.javaw.callplatform.util.FinishListener;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Toast;

@SuppressLint("WorldReadableFiles")
public class Login extends Activity {

	// 定义界面中两个文本框
	EditText Eusername, Epassword;
	// 定义界面中三个按钮
	Button Blogin, Bforget, Bexit;
	// 定义界面中两个复选框
	CheckBox Bautologin,Brememberpassword;
	
	SharedPreferences preferences;
	SharedPreferences.Editor editor;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		
		// 获取只能被本应用程序读、写的SharedPreferences对象
		preferences = getSharedPreferences("net.javaw.callplatform", MODE_WORLD_READABLE);
		editor = preferences.edit();
		
		Bautologin = (CheckBox) findViewById(R.id.autologin);
		Brememberpassword = (CheckBox) findViewById(R.id.rememberpassword);
		
		Eusername = (EditText) findViewById(R.id.username);
		Epassword = (EditText) findViewById(R.id.password);
		
		
		//设置用户名
		if(null!=preferences.getString("username", null)){
			Eusername.setText(preferences.getString("username", null));
		}
		
		//设置密码
		if(null!=preferences.getString("rememberpassword", null)&&null!=preferences.getString("password", null)){
			
			Brememberpassword.setChecked(true);
			Epassword.setText(preferences.getString("password", null));
		}
		
		if(null!=preferences.getString("autologin", null)&&null!=preferences.getString("username", null)&&null!=preferences.getString("password", null))
		{
			Bautologin.setChecked(true);
			
			String username = preferences.getString("username", null);
			String password = preferences.getString("password", null);
			// 执行输入校验
			if (validate(username,password))
			{
				if(null==preferences.getString("logout", null))
				{
					// 如果登录成功
					if (loginValidate(username,password))
					{
						// 删除旧的用户名
						editor.remove("username");
						// 存入当前用户名
						editor.putString("username", Eusername.getText().toString());
						// 提交所有存入的数据
						editor.commit();
						
						// 启动Main Activity
						Intent intent = new Intent(Login.this, Notifications.class);
						startActivity(intent);
						// 结束该Activity
						finish();
					}
					else
					{
						DialogUtil.showDialog(Login.this, "用户名称或者密码错误，请重新输入！", false);
					}
				}else
				{
					// 删除注销标识
					editor.remove("logout");
					
					// 提交所有存入的数据
					editor.commit();
				}
				
			}
		}
		
		Bautologin.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				
				if(isChecked)
				{
					// 删除旧的自动登录标识
					editor.remove("autologin");
					// 存入自动登录标识
					editor.putString("autologin", "true");
					// 提交所有存入的数据
					editor.commit();
					
					Toast.makeText(getApplicationContext(), "自动登录设置成功！", Toast.LENGTH_SHORT).show();
					
					Brememberpassword.setChecked(true);
				}else
				{
					// 删除旧的自动登录标识
					editor.remove("autologin");
					// 提交所有存入的数据
					editor.commit();
					
					Toast.makeText(getApplicationContext(), "取消自动登录设置成功！", Toast.LENGTH_SHORT).show();
				}
				
				
			}
		});
		
		
		Brememberpassword.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				
				if(isChecked)
				{
					// 删除旧的自动登录标识
					editor.remove("rememberpassword");
					// 存入自动登录标识
					editor.putString("rememberpassword", "true");
					// 提交所有存入的数据
					editor.commit();
					
					Toast.makeText(getApplicationContext(), "记住密码设置成功！", Toast.LENGTH_SHORT).show();
				}else
				{
					// 删除旧的自动登录标识
					editor.remove("rememberpassword");
					// 提交所有存入的数据
					editor.commit();
					
					Toast.makeText(getApplicationContext(), "取消记住密码设置成功！", Toast.LENGTH_SHORT).show();
					
					Bautologin.setChecked(false);
				}
			}
		});
		
		Blogin = (Button) findViewById(R.id.login);
		Bforget = (Button) findViewById(R.id.forget);
		Bexit = (Button) findViewById(R.id.exit);
		
		Bforget.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Toast.makeText(getApplicationContext(), "待扩展。。。\n\n河南卓辉通信技术有限公司版权所有\n\n免责声明\n\n河南卓辉通信版权所有\n\n作者：史守阳\n\n仅限河南卓辉通信内部员工使用", Toast.LENGTH_LONG).show();
			}
		});
		Bexit.setOnClickListener(new FinishListener(this));
		Blogin.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String username = Eusername.getText().toString();
				String password = Epassword.getText().toString();
				// 执行输入校验
				if (validate(username,password))
				{
					// 如果登录成功
					if (loginValidate(username,password))
					{
						// 删除旧的用户名
						editor.remove("username");
						// 存入当前用户名
						editor.putString("username", Eusername.getText().toString());
						// 删除旧的密码
						editor.remove("password");
						// 存入当前密码
						editor.putString("password", Epassword.getText().toString());
						// 提交所有存入的数据
						editor.commit();
						
						// 启动Main Activity
						Intent intent = new Intent(Login.this, Notifications.class);
						startActivity(intent);
						// 结束该Activity
						finish();
					}
					else
					{
						DialogUtil.showDialog(Login.this, "用户名称或者密码错误，请重新输入！", false);
					}
				}
			}
		});
	}

	private boolean loginValidate(String username ,String password)
	{
		Boolean flag = false;
		
		if("drow".equals(username)&&"111111".equals(password)){
			flag = true;
		}

		return flag;
	}

	// 对用户输入的用户名、密码进行校验
	private boolean validate(String username ,String password)
	{
		if ("".equals(username))
		{
			DialogUtil.showDialog(this, "平台账号是必填项！", false);
			return false;
		}
		if ("".equals(password))
		{
			DialogUtil.showDialog(this, "服务密码是必填项！", false);
			return false;
		}
		return true;
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		Toast.makeText(getApplicationContext(), "待扩展。。。\n\n河南卓辉通信技术有限公司版权所有\n\n免责声明\n\n河南卓辉通信版权所有\n\n作者：史守阳\n\n仅限河南卓辉通信内部员工使用", Toast.LENGTH_LONG).show();
		return true;
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) 
		{
			 super.onKeyDown(keyCode, event);
			 
		}else if(keyCode == KeyEvent.KEYCODE_MENU) 
		{
			Toast.makeText(getApplicationContext(), "待扩展。。。\n\n河南卓辉通信技术有限公司版权所有\n\n免责声明\n\n河南卓辉通信版权所有\n\n作者：史守阳\n\n仅限河南卓辉通信内部员工使用", Toast.LENGTH_LONG).show();
		}
		return true;
	}
}
